import {
  PutDto,
  PutRes,
  PageDto,
  PageData,
  DisableDto,
  DisableRes,
  PostMutiRes,
  PostMutiDto,
  DisableUUid32Dto,
  DisableUUid32Res,
  PutMutiDto,
  PutMutiRes,
} from "~/rest";
import { Tradable } from "~/extend";
import { Sortable } from "~/search.match";
import { PageResult } from "~/search";
import { WithPasterList } from "~/view.with";

import { Gateway, getGateway } from "@/droplet-bus/core.define";
import { ENUM_APP } from "@/droplet-bus/core.define";
import { Book } from "@/pond-book/schema.book";

import { app_gateway } from "./core.local";
import { ENUM_VOUCHER } from "./core.define";
import { Voucher, VoucherGroup } from "./schema.voucher";
import { Media } from "@/pond-media/schema.media";

export const VoucherGroupGateway: Gateway = getGateway(app_gateway, "river_voucher_group");
export type putVoucherGroupDto = PutMutiDto<VoucherGroup>;
export type disableVoucherGroupDto = DisableDto;

export interface VoucherGroupInterfaceWithPrimary<T> {
  get(dto: T): Promise<PageResult<VoucherGroup>>;
  put(dto: T & putVoucherGroupDto): Promise<PutMutiRes<VoucherGroup>>;
  disable(dto: T & disableVoucherGroupDto): Promise<DisableRes>;
}

export const VoucherGateway: Gateway = getGateway(app_gateway, "river_voucher");
export type getVoucherDto = PageDto<Voucher> & { groupId: VoucherGroup["id"]; decimal: number };
export type getVoucherByIdDto = { uuid32?: Voucher["uuid32"]; tradeId?: Voucher["tradeId"] };
export type getByTradeIdDto = { tradeIDs: Voucher["tradeId"][] };
export type getVoucherAfterBookSortDto = PageDto<Voucher> & { groupId: VoucherGroup["id"] } & Pick<Book, "type" | "direction"> & { sorter: Sortable };

export interface VoucherReadInterfaceWithGrant<T> {
  get(dto: T & getVoucherDto): Promise<PageData<WithPasterList & Voucher>>;

  getById(dto: T & getVoucherByIdDto): Promise<{ first: Voucher; comboBooks: Book[] }>;

  /** 根据-交易号-查询财务记录 */
  getByTradeId(dto: T & getByTradeIdDto): Promise<PageData<WithPasterList & Voucher>>;

  /** 根据账簿金额进行排序 */
  getVoucherAfterBookSort(dto: T & getVoucherAfterBookSortDto): Promise<PageData<WithPasterList & Voucher>>;
}

export type postVoucherDto = PostMutiDto<Voucher & { bankName?: string }> & {
  voucherGroupId: VoucherGroup["id"];
  voucherType: ENUM_VOUCHER;
};
export type putVoucherDto = PutDto<Voucher>;
export type disableVoucherDto = DisableUUid32Dto;

export type patchVoucherAppBookDto = { app: ENUM_APP; voucherId: Voucher["uuid32"]; tradableList: (Tradable & { amount: number })[] };

export interface VoucherWriteInterfaceWithGrant<T> {
  post(dto: T & postVoucherDto): Promise<PostMutiRes<Voucher>>;
  put(dto: T & putVoucherDto): Promise<PutRes<Voucher>>;
  disable(dto: T & disableVoucherDto): Promise<DisableUUid32Res>;

  /** 在一个财务记录上
   * @step1 删除衍生的账簿记录
   * @step2 根据 -App类型- 以及交易号补充衍生账簿记录
   */
  patchVoucherAppBook(dto: T & patchVoucherAppBookDto): Promise<null>;
}
